<?php 
/**
 *   Joomdiet - administrator/components/views/foodcategories/tmpl/default.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') or die('Restricted access'); ?>
<form action="index.php" method="post" name="adminForm">
<div id="editcell">
    <table class="adminlist">
        <thead>
            <tr>
                <th width="20">
                    <input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($this->items); ?>);" />
                </th>
                <th width="5">
                    <?php echo JText::_('ID'); ?>
                </th>
                <th width="350">
                    <?php echo JText::_('COM_JOOMDIET_FOOD_CATEGORIES_NAME'); ?>
                </th>
                <th>
                    <?php echo JText::_('COM_JOOMDIET_FOOD_CATEGORIES_DESCRIPTION'); ?>
                </th>
                <th width="30">
                    <?php echo JText::_('COM_JOOMDIET_DELETE'); ?>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php $k = 0; $i = 0; foreach($this->items as $row) {
                displayTree($this->model, $row, $k, $i, 0);    
            $k = 1 - $k; $i++; } ?>
        </tbody>
    </table>
</div>

<!-- tree display constructor -->
<?php
function displayTree($model, $node, $k, $i, $level) { 
    $row = $node['data'];
    $checked = JHTML::_('grid.id', $i, $row->id);
    $link = JRoute::_('index.php?option=com_joomdiet&'.
                                'controller=foodcategories&'.
                                'task=editFoodCategory&'.
                                'cid[]='.$row->id);
    $delete_link = JRoute::_('index.php?option=com_joomdiet&'.
                                       'controller=foodcategories&'.
                                       'task=directDeleteFoodCategory&'.
                                       'cid[]='.$row->id);
    ?>
    <tr class="<?php echo 'row'.$k; ?>">
        <td>
            <?php if ($model->isLeaf($node)) { echo $checked; } ?>
        </td>
        <td>
            <?php if ($model->isLeaf($node)): ?>
                <?php echo $row->id; ?>
            <?php else: ?>
                <b><?php echo $row->id; ?></b>
            <?php endif; ?>
        </td>
        <td>
            <b>
            <?php for($p = 0; $p < $level; $p++): ?>
                &nbsp;&nbsp;&nbsp;&nbsp;
            <?php endfor; ?></b>
            <?php if ($model->isLeaf($node)): ?>
                <a href="<?php echo $link; ?>"><?php echo $row->name; ?></a>
            <?php else: ?>
                <b><a href="<?php echo $link; ?>"><?php echo $row->name; ?></a></b>
            <?php endif; ?>
        </td>
        <td>
            <?php echo $row->description; ?>
        </td>
        <td>
            <a href="<?php echo $delete_link; ?>"><?php echo JText::_('COM_JOOMDIET_DELETE'); ?></a>
        </td>
    </tr>
    <?php 
        if (!$model->isLeaf($node)) {
            foreach ($node['children'] as $n) {
                displayTree($model, $n, 1-$k, $i++, $level+1);
            }
        }
    ?>
<?php }
?>

<input type="hidden" name="option" value="com_joomdiet" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="foodcategories" />
<input type="hidden" name="view" value="foodcategories" />

</form>
